package com.ykdl.member.kid.fittool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.dictionary.DictionaryDataManager;
import com.ykdl.member.http.Network;
import com.ykdl.member.kid.beans.BreastFeeding;
import com.ykdl.member.kid.beans.Feeding;
import com.ykdl.member.kid.beans.StatuResult;
import com.ykdl.member.kid.util.AssertTool;
import com.ykdl.member.kid.util.CalendarUtil;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.views.ArrayWheelAdapter;
import com.ykdl.member.views.DateWheelAdapter;
import com.ykdl.member.views.NumericWheelAdapter;
import com.ykdl.member.views.WheelView;

public class AddFeeding implements OnClickListener{
	
	private Context context;
	private ViewGroup group;
	private RefreshListView mListView;
	private TextView mTxtTitle;
	private TextView mTxtTypeAmount, mTxtDuration, mTxtVomits, mTxtTime;
	private Button mBtnClose, mBtnBack, mBtnNext, mBtnComplate;
	private View mLayerFeedType, mLayerDuration, mLayerVomits, mLayerTime;
	private WheelView mWheelView1, mWheelView2,mWheelView3,mWheelView4,mWheelView5,mWheelView6,mWheelView7,mWheelView8;
	
	private List<String> feed_type;
	private Feeding feed = new Feeding();
	private DateWheelAdapter mDateAdapter;
	private CalendarUtil mCalendarUtil;
	private ProgressDialog mProgressDialog;
	
	private OnRefreshListListener mListListener;
	
	public AddFeeding(Context context, ViewGroup group, RefreshListView listview){
		this.context = context;
		this.group = group;
		this.mListView = listview;
		this.mListListener = (OnRefreshListListener)context;
		View view = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_feed_tool_feeding, group);
		
		mCalendarUtil = new CalendarUtil();
		
		view.findViewById(R.id.imgFeedNotify).setOnClickListener(this);
		
		mTxtTypeAmount = (TextView) view.findViewById(R.id.txtTypeAmount);
		mTxtDuration = (TextView) view.findViewById(R.id.txtDuration);
		mTxtVomits = (TextView) view.findViewById(R.id.txtVomits);
		mTxtTime = (TextView) view.findViewById(R.id.txtTime);
		
		mTxtTitle = (TextView) view.findViewById(R.id.txtTitle);
		mTxtTitle.setText("喂奶类型");
		
		mBtnClose = (Button) view.findViewById(R.id.btnClose);
		mBtnClose.setOnClickListener(this);
		mBtnBack = (Button) view.findViewById(R.id.btnBack);
		mBtnBack.setOnClickListener(this);
		mBtnNext = (Button) view.findViewById(R.id.btnNext);
		mBtnNext.setOnClickListener(this);
		mBtnComplate = (Button) view.findViewById(R.id.btnComplate);
		mBtnComplate.setOnClickListener(this);
		
		
		mLayerFeedType = view.findViewById(R.id.layerFeedType);
		feed_type = DictionaryDataManager.getInstance().getParentDataList(context, AssertTool.FEED_TYPE);
		mWheelView1 = (WheelView) view.findViewById(R.id.wheelView1);
		String[] items = new String[feed_type.size()];
		feed_type.toArray(items);
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(items);
		mWheelView1.setAdapter(adapter);
		mWheelView1.getCurrentItem();
		mWheelView2 = (WheelView) view.findViewById(R.id.wheelView2);
		mWheelView2.setAdapter(new ArrayWheelAdapter<String>(WeiYanData.getRongliang()));
		mWheelView2.setLabel("ml");
		
		
		mLayerDuration = view.findViewById(R.id.layerDuration);
		mWheelView3 = (WheelView) view.findViewById(R.id.wheelView3);
		mWheelView3.setAdapter(new NumericWheelAdapter(1, 60));
		mWheelView3.setLabel("分钟");
		
		
		mLayerVomits = view.findViewById(R.id.layerVomits);
		mWheelView4 = (WheelView) view.findViewById(R.id.wheelView4);
		String[] vomits = {"无吐奶", "有吐奶"};
		mWheelView4.setAdapter(new ArrayWheelAdapter<String>(vomits));
		
		
		mLayerTime = view.findViewById(R.id.layerTime);
		mWheelView5 = (WheelView) view.findViewById(R.id.wheelView5);
		String[] strs = {"开吃时间", "吃完时间"};
		mWheelView5.setAdapter(new ArrayWheelAdapter<String>(strs));
		mWheelView6 = (WheelView) view.findViewById(R.id.wheelView6);
		Calendar end = Calendar.getInstance();
		long endtime = end.getTimeInMillis();
		mDateAdapter = new DateWheelAdapter(endtime-(24*60*60*1000*14), endtime);
		mWheelView6.setAdapter(mDateAdapter);
		mWheelView7 = (WheelView) view.findViewById(R.id.wheelView7);
		mWheelView7.setAdapter(new NumericWheelAdapter(0, 23));
		mWheelView8 = (WheelView) view.findViewById(R.id.wheelView8);
		mWheelView8.setAdapter(new NumericWheelAdapter(0, 59));
		
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgFeedNotify:
			Intent intent = new Intent(context, FeedNotifyActivity.class);
			context.startActivity(intent);
			break;
		case R.id.btnClose:
			group.removeAllViews();
			group.setVisibility(View.GONE);
			mListView.setEnabled(true);
			break;
		case R.id.btnBack:
			
			if (mLayerTime.getVisibility() == View.VISIBLE) {
				mLayerTime.setVisibility(View.GONE);
				mLayerVomits.setVisibility(View.VISIBLE);
				mBtnComplate.setVisibility(View.GONE);
				mBtnNext.setVisibility(View.VISIBLE);
				mBtnBack.setVisibility(View.VISIBLE);
				mTxtTitle.setText("有无吐奶");
			} else if (mLayerVomits.getVisibility() == View.VISIBLE) {
				mLayerVomits.setVisibility(View.GONE);
				mLayerDuration.setVisibility(View.VISIBLE);
				mTxtTitle.setText("喂奶时间");
			} else if (mLayerDuration.getVisibility()==View.VISIBLE) {
				mLayerDuration.setVisibility(View.GONE);
				mLayerFeedType.setVisibility(View.VISIBLE);
				mBtnBack.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.VISIBLE);
				mBtnNext.setVisibility(View.VISIBLE);
				mTxtTitle.setText("喂奶类型");
			}
			
			break;
		case R.id.btnNext:
			if (mLayerFeedType.getVisibility() == View.VISIBLE) {
				int item1 = mWheelView1.getCurrentItem();
				feed.setFeeding_type(item1+1);
				int item2 = mWheelView2.getCurrentItem();
				int amount = 10+10*item2;
				feed.setAmount(amount);
				mTxtTypeAmount.setText(feed_type.get(item1)+": "+amount+"ml");
				mLayerFeedType.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.GONE);
				mBtnBack.setVisibility(View.VISIBLE);
				mLayerDuration.setVisibility(View.VISIBLE);
				mTxtTitle.setText("喂奶时间");
			} else if (mLayerDuration.getVisibility() == View.VISIBLE) {
				
				int value3 =mWheelView3.getCurrentItem()+1;
				feed.setDuration(value3);
				mTxtDuration.setText(value3+"分钟");
				mLayerDuration.setVisibility(View.GONE);
				mLayerVomits.setVisibility(View.VISIBLE);
				mTxtTitle.setText("有无吐奶");
				
			} else if (mLayerVomits.getVisibility() == View.VISIBLE) {
				
				int item4 = mWheelView4.getCurrentItem();
				feed.setVomits(item4);
				mTxtVomits.setText(feed.getVomits()==0?"无吐奶":"有吐奶");
				
				mLayerVomits.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.GONE);
				mBtnBack.setVisibility(View.VISIBLE);
				mBtnNext.setVisibility(View.INVISIBLE);
				mBtnComplate.setVisibility(View.VISIBLE);
				mLayerTime.setVisibility(View.VISIBLE);
				mTxtTitle.setText("开喝/喝完时间");
				
			}
			break;
		case R.id.btnComplate:
			if (mLayerTime.getVisibility() == View.VISIBLE) {
				int item5 = mWheelView5.getCurrentItem();
				String month, day;
				String strDate = mDateAdapter.getItem(mWheelView6.getCurrentItem());
				if (strDate.equals("今天")) {
					String[] str = new SimpleDateFormat("MM-dd").format(new Date()).split("-");
					month = str[0];
					day = str[1];
				} else {
					String[] str = strDate.split("月");
					month = str[0];
					day = str[1].replace("日", "");
				}
				int hour = mWheelView7.getCurrentItem();
				int minute = mWheelView8.getCurrentItem();
				long time = mCalendarUtil.getLongByString(month, day, String.valueOf(hour), String.valueOf(minute));
				if (item5 == 0) {
					feed.setBegin_time(time/1000);
					feed.setEnd_time(time/1000+feed.getDuration()*60);
				} else {
					feed.setEnd_time(time/1000);
					feed.setBegin_time(time/1000 - feed.getDuration()*60);
				}
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				mTxtTime.setText(sdf.format(new Date(time)));
				submitData(feed);
			}
			break;
		default:
			break;
		}
	}
	
	private void submitData(Feeding breast) {
		mProgressDialog = ProgressDialog.show(context, null, "正在提交...");
		Network.getInstance().addFeedBean(breast, new AddDataTag());
	}
	
	private class AddDataTag implements ITag<StatuResult>{

		@Override
		public void refreshUI(StatuResult t, DataState ds) {
			mProgressDialog.dismiss();
			if (t != null) {
				// 是否刷新视图 ？？
				group.removeAllViews();
				group.setVisibility(View.GONE);
				mListView.setEnabled(true);
				mListListener.refreshList();
			} else {
				Toast.makeText(context, "添加数据失败", 1).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			mProgressDialog.dismiss();
			Toast.makeText(context, "添加数据失败", 1).show();
			return true;
		}

		@Override
		public void refreshCancel() {
			
		}
		
	}
	
}
